<template>
  <div>
    <el-button type="button" class="addbtn" @click="addNew">新增剧集</el-button>

    <el-dialog title="新增剧集" v-model="$store.state.dialogFormVisible" size="tiny">
      <el-form label-width="80px">
        <el-form-item label="剧集名称">
          <el-input v-model="$store.state.dramasEntry.title" placeholder="剧集名称"></el-input>
        </el-form-item>
        <el-form-item label="封面图片">
          <el-input v-model="$store.state.dramasEntry.imgurl" placeholder="图片地址"></el-input>
        </el-form-item>
        <el-form-item label="星级评分">
          <el-select v-model="$store.state.dramasEntry.star" placeholder="请选择评分">
            <el-option label="三星级" value="3"></el-option>
            <el-option label="两星级" value="2"></el-option>
            <el-option label="一星级" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="dialogState(false)">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
    export default{

        methods:{
          onSubmit(){

            var path;
            if(this.$route.path === '/'){
              path = '/wanted';
            }else{
              path= this.$route.path
            }

            var type = path.substring(1);

            let data = {
              id : this.$store.state.dramasEntry.id,
              title: this.$store.state.dramasEntry.title,
              imgurl:this.$store.state.dramasEntry.imgurl,
              type:type,
              star:this.$store.state.dramasEntry.star,
            };

            let data2 = {
              id : this.$store.state.dramasEntry.id,
              title: this.$store.state.dramasEntry.title,
              imgurl:this.$store.state.dramasEntry.imgurl,
              type:this.$store.state.dramasEntry.type,
              star:this.$store.state.dramasEntry.star,
            };

            let updateInfo = {
               index :  this.$store.state.dramasIndex,
               data : data
            }

            if(this.$store.state.dramasEntry.edit){
                this.$http.post('http://localhost:8888/update', data2).then(function (ret) {
                })
                this.$store.commit('updateDramas',updateInfo);
                this.$store.commit('resetValue',{});
                this.$message({
                  message: '修改剧集成功',
                  duration:1500
                });
            }else{
              this.$http.post('http://localhost:8888/create',data).then(function (ret) {
                let dramasEntry = this.$store.state.dramasEntry
                this.$store.commit('addDramas', dramasEntry);
              })
              this.$message({
                message: '添加剧集成功',
                  type:'success',
                  duration:1500
              });
            }
            this.dialogState(false);
          },
          dialogState(state){
             this.$store.commit('dialogState',state);
          },
          addNew(){
            this.$store.commit('resetValue',{});
            this.dialogState(true);
          }
        }
    }
</script>
<style>
.addbtn{
   position:fixed;
   top:20px;
   right:20px;
   z-index:999
}
</style>
